#include "mainframe.h"

MainFrame::MainFrame(QWidget *parent)
    : QDialog(parent), ui(new Ui::MainFrameClass)
{
    QStringList argv = qApp->arguments();
    if(argv.length()>1)
    {
        ui->setupUi(this);
        QString settingName(qApp->arguments()[1]);
        setWindowFlags(Qt::SplashScreen);
        ui->settingLabel->setText(settingName);
        QTimer::singleShot(100, this, SLOT(deferredInitialization()));
    }
    else
    {
        // Immediate kill.
        QTimer::singleShot(10, qApp, SLOT(quit()));
    }
}

MainFrame::~MainFrame()
{
    delete ui;
}

//! Deferred initialization.
void MainFrame::deferredInitialization()
{
    // One second.
    int visibilityTime=1000;
    QString settingName = ui->settingLabel->text();
    QString acpiFilename("/proc/acpi/ibm/"+settingName);
    if(QFile::exists(acpiFilename))
    {
        QFile statusFile(acpiFilename);
        QRegExp patternMaximum("<level> is \\d+-(\\d+)");
        int currentValue=0, maximumValue=0;
        bool muted=false;
        if(statusFile.open(QFile::ReadOnly))
        {
            QTextStream textStream;
            textStream.setDevice(&statusFile);
            QString line;
            do
            {
                line = textStream.readLine();
                //! Check current level.
                if(line.startsWith("level:"))
                {
                    QStringList parts(line.split(':'));
                    bool result=true;
                    int convertedValue=parts[1].toInt(&result);
                    if(result)
                    {
                        //! Conversion OK, set current value.
                        currentValue=convertedValue;
                    }
                    continue;
                }
                //! Capture maximum value for current status.
                if(patternMaximum.indexIn(line)>=0)
                {
                    bool result=true;
                    int convertedValue=patternMaximum.cap(1).toInt(&result);
                    if(result)
                    {
                        //! Conversion OK, set current value.
                        maximumValue=convertedValue;
                    }
                    continue;
                }
                //! Audio special case: mute.
                if(line.startsWith("mute:") && line.indexOf("on")>0)
                {
                    muted=true;
                    continue;
                }
            } while(!line.isNull());
        }

        //! Adjust current and maximum values.
        if(maximumValue<currentValue)
        {
            maximumValue=currentValue;
        }
        ui->progressBar->setValue(currentValue);
        ui->progressBar->setMaximum(maximumValue);

        //! Tune for mute.
        if(muted)
        {
            ui->settingLabel->setText(ui->settingLabel->text()+" muted");
        }

        // Show the window.
        show();
    }
    else
    {
        visibilityTime=10;
    }

    // Set up a one shot timer to kill this app.
    QTimer::singleShot(visibilityTime, qApp, SLOT(quit()));
}

//! Entry point.
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainFrame w;
    return a.exec();
}

//! End of file.
